package com.budwk.app.sqfwV2.component;

import com.budwk.app.common.component.DictComponent;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.constants.DictConstants;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskParamDataDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.AccommodatePeopleVo;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.SandwichingVo;
import com.budwk.app.sqfwV2.enums.RectificateTimeEnum;
import com.budwk.app.sqfwV2.enums.dayingRoad.DayingRoadProblemEnum;
import com.budwk.app.sqfwV2.enums.dayingRoad.OtherPatrolContentsEnum;
import com.budwk.app.sys.models.Sys_dict;
import com.budwk.app.sys.services.SysDictService;
import com.budwk.app.sys.vo.SysDictVO;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;

import java.util.List;
import java.util.Optional;

/**
 * 消防问题实现类
 */

public class FireIssusComponent {

    private static boolean isExistSandwichSafeProblem(SafeCheckTaskParamDataDTO s, String defaultValue) {
        if (s == null) {
            return false;
        }
        if (defaultValue.equals(s.getKey())) {
            return true;
        }
        return false;
    }
    private static boolean isLastExist(FireSafetyOnDayingRoadPdfDTO lastDataPdfDTO) {
        if (lastDataPdfDTO == null) {
            return false;
        }
        SandwichingVo lastS = lastDataPdfDTO.getSandwichConstructionUse();
        if (lastS == null) {
            return false;
        }
        return true;
    }

    /**
     * 是否搭建夹层
     * @param pdfDTO
     */
    public static void buildSandwichingVo(FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        SandwichingVo s = pdfDTO.getSandwichConstructionUse();
        if (s == null) {
            return;
        }
        // 1 夹层楼板
        SafeCheckTaskParamDataDTO m = s.getMezzaninFfloorSlab();
        if (isExistSandwichSafeProblem(m, DictConstants.MEZZANINE_FLOOR_SLAB_COMBUSTIBLE)) {
            m.setIsRectification(RectificateTimeEnum.isNowRectification(m.getRectificationTypeCode()))
                    .setProblemDesc("夹层楼板为可燃材料");
        }
        // 2 楼梯建筑材料
        SafeCheckTaskParamDataDTO mbm = s.getStaircaseBuildingMaterials();
        if (isExistSandwichSafeProblem(mbm, DictConstants.STAIRCASE_BUILD_MATERIALS_COMBUSTIBLE)) {
            mbm.setIsRectification(RectificateTimeEnum.isNowRectification(mbm.getRectificationTypeCode()))
                    .setProblemDesc("楼梯建筑材料为可燃材料");
        }
    }

    // 是否存在住人问题隐患
    public static boolean existAccommodatePeopleProblem(FireSafetyOnDayingRoadPdfDTO roadPdfDTO) {
        AccommodatePeopleVo a = roadPdfDTO.getAccommodatePeople();
        if (a == null) {
            return false;
        }
        String residentialType = a.getResidentialType();
        if (StringUtils.isEmpty(residentialType)) {
            return false;
        }
        Optional<String> first = DictComponent.getResidentNoForms().stream().filter(e -> e.equals(residentialType)).findFirst();
        if (first.isEmpty()) {
            return false;
        }
        // 存在住人问题  ->  需要判断多高层生产经营场所
        // 是否选中多高层生产经营场所多高层生产经营场所
        String buildingType = roadPdfDTO.getBuildingType();
        if (StringUtils.isNotEmpty(buildingType) && buildingType.equals(DictConstants.MULTI_BUILDINGS)) {
            return false;
        }
        return true;
    }


    /**
     * 是否住人
     */
    public static void buildAccommodatePeopleVo(FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        AccommodatePeopleVo a = pdfDTO.getAccommodatePeople();
        SysDictService dictService = NutzIocUtils.getBean(SysDictService.class);
        if (a == null) {
            return;
        }
        // 如果经营范围是旅馆 直接返回
        if (DictConstants.MAIN_BUSINESS_SCOPE_HOTEL.equals(pdfDTO.getMainBusinessScope())) {
            a.setProblemDesc(null);
            a.setIsRectification(null);
            return;
        }
        if (existAccommodatePeopleProblem(pdfDTO)) {
            Sys_dict sysDict = dictService.fetch(Cnd.where(Sys_dict::getCode, "=", a.getResidentialType()));
            a.setProblemDesc("该场所的住人形式为" + sysDict.getName());
            a.setIsRectification(RectificateTimeEnum.isNowRectification(a.getRectificateType()));
            return;
        }
        // 如果是已整改
        if (DictConstants.RESIDENTS_FORM_RECTIFIED.equals(a.getResidentialType())) {
            a.setIsRectification(true);
        }
    }

    /**
     * 灭火器
     * @param pdfDTO
     */
    public static void buildEquippedFire(FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        SafeCheckTaskParamDataDTO wef = pdfDTO.getWhetherEquippedFire();
        if (wef == null) {
            // 给默认值
            wef = new SafeCheckTaskParamDataDTO();
            wef.setKey(DayingRoadProblemEnum.FORE.getDefaultValue()).setFields(DayingRoadProblemEnum.FORE.getCode());
        }
        wef.setRiskLevel(DayingRoadPdfComponent.getDictName(wef.getRiskLevelCode(), null))
                .setRectificationType(DayingRoadPdfComponent.getDictName(wef.getRectificationTypeCode(), null));
        List<SafeCheckTaskParamDataDTO> otherPatrolContents = pdfDTO.getOtherPatrolContents();
        OtherPatrolContentsEnum fEnum = OtherPatrolContentsEnum.FORETEEN;
        SafeCheckTaskParamDataDTO sc = otherPatrolContents.stream().filter(e -> e.getFields().equals(fEnum.getCode())).findAny().get();
        // 和默认值一致
        if (DayingRoadProblemEnum.FORE.getDefaultValue().equals(wef.getKey())) {
            copeEquippedFire(wef, sc);
            sc.setKey(fEnum.getDefaultValue());
            return;
        }
        // 和默认值不一致
        copeEquippedFire(wef, sc);
        sc.setKey("true").setIsRectification(RectificateTimeEnum.isNowRectification(wef.getRectificationTypeCode())).setProblemDesc("未配备灭火器");
        wef.setIsRectification(RectificateTimeEnum.isNowRectification(wef.getRectificationTypeCode())).setProblemDesc("未配备灭火器");
    }

    private static void copeEquippedFire(SafeCheckTaskParamDataDTO wef, SafeCheckTaskParamDataDTO sc) {
        OtherPatrolContentsEnum fEnum = OtherPatrolContentsEnum.FORETEEN;
        sc.setFields(fEnum.getCode())
                .setRiskDesc(fEnum.getDesc())
                .setRiskCode(fEnum.getNumber())
                .setRectificationMeasure(wef.getRectificationMeasure())
                .setRiskLevelCode(wef.getRiskLevelCode())
                .setRectificationTypeCode(wef.getRectificationTypeCode())
                .setRiskLevel(wef.getRiskLevel())
                .setRectificationType(wef.getRectificationType())
                .setRiskImages(wef.getRiskImages())
                .setRectificationBackPhoto(wef.getRectificationBackPhoto());
    }


    /**
     * 电气线路情况
     */
    public static void buildElectricalCircuitSituation(FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        SafeCheckTaskParamDataDTO se = pdfDTO.getElectricalCircuitSituation();
        if (se == null) {
            return;
        }
        String key = se.getKey();
        SysDictService dictService = NutzIocUtils.getBean(SysDictService.class);
        if (DictComponent.electricalCircuitSituationList().contains(key)) {
            Sys_dict sysDict = dictService.fetch(Cnd.where(Sys_dict::getCode, "=", key));
            se.setIsRectification(RectificateTimeEnum.isNowRectification(se.getRectificationTypeCode())).setProblemDesc("电气线路" + sysDict.getName());
        }
    }
    /**
     * 电动自行车使用情况
     */
    public static void buildElectricBicyclesUse(FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        SafeCheckTaskParamDataDTO s = pdfDTO.getElectricBicyclesUse();
        if (s == null) {
            return;
        }
        List<String> bicyclesIndoor = List.of(DictConstants.BICYCLES_INDOOR, DictConstants.BICYCLES_OUTDOOR, DictConstants.BICYCLES_FLYWIRE, DictConstants.BICYCLES_OTHER);
        SysDictService dictService = NutzIocUtils.getBean(SysDictService.class);
        if (bicyclesIndoor.contains(s.getKey())) {
            Sys_dict sysDict = dictService.fetch(Cnd.where(Sys_dict::getCode, "=", s.getKey()));
            s.setIsRectification(RectificateTimeEnum.isNowRectification(s.getRectificationTypeCode())).setProblemDesc("电动自行车进行" + sysDict.getName());
        }
    }

    /**
     * 明火、高温加热、取暖设备等的使用地点
     */
    public static void buildFireHighUsageArea(FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        SafeCheckTaskParamDataDTO s = pdfDTO.getFireHighUsageArea();
        if (s == null) {
            return;
        }
        List<String> list = List.of(DictConstants.FLAMES_HIGH_HEAT_SAND, DictConstants.FLAMES_HIGH_HEAT_OTHER);
        if (list.contains(s.getKey())) {
            s.setIsRectification(RectificateTimeEnum.isNowRectification(s.getRectificationTypeCode()))
                    .setProblemDesc("使用明火、高温加热、取暖设备");
        }
    }


    /**
     * 是否存放易燃易爆危险品
     */
    public static void buildCombustiblEexplosive(FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        SafeCheckTaskParamDataDTO s = pdfDTO.getIsCombustiblEexplosive();
        if (s == null) {
            return;
        }
        if (!DayingRoadProblemEnum.EIGHT.getDefaultValue().equals(s.getKey())) {
            s.setIsRectification(RectificateTimeEnum.isNowRectification(s.getRectificationTypeCode()))
                    .setProblemDesc("存放有易燃易爆危险品");
        }
    }

    /**
     * 是否存放易燃易爆危险品
     */
    public static void buildOtherDanger(FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        List<SafeCheckTaskParamDataDTO> others = pdfDTO.getOtherHiddenDangers();
        if (others == null || others.size() == 0) {
            return;
        }
        SafeCheckTaskParamDataDTO s = others.get(others.size() - 1);
        if (!DayingRoadProblemEnum.TWELVE.getDefaultValue().equals(s.getKey())) {
            s.setIsRectification(RectificateTimeEnum.isNowRectification(s.getRectificationTypeCode()))
                    .setProblemDesc(StrUtil.formatStr(s.getRectificationMeasure()));
        }
    }

    /**
     * 是否存放易燃易爆危险品
     */
    public static void buildFireOtherIssues(FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        SafeCheckTaskParamDataDTO fireOtherIssues = pdfDTO.getFireOtherIssues();
        SafeCheckTaskParamDataDTO safe = DayingRoadProblemEnum.initData(DayingRoadProblemEnum.THIRTEEN);
        if (fireOtherIssues == null) {
            safe.setRectificationMeasure("无");
            pdfDTO.setFireOtherIssues(safe);
            return;
        }
        String rectificationMeasure = fireOtherIssues.getRectificationMeasure();
        if (StringUtils.isEmpty(rectificationMeasure) || "无".equals(rectificationMeasure)) {
            safe.setRectificationMeasure("无");
            pdfDTO.setFireOtherIssues(safe);
            return;
        }
        fireOtherIssues.setKey("true").setIsRectification(false).setProblemDesc(rectificationMeasure);
    }
}
